package com.tianfeng.huayi.controller;

import com.tianfeng.huayi.entity.ComplaintManagement;
import com.tianfeng.huayi.entity.Owner;
import com.tianfeng.huayi.service.IComplaintManagementService;
import com.tianfeng.huayi.util.CaptchaGenerator;
import com.tianfeng.huayi.util.CurrentDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/complaintManagement")
@CrossOrigin
public class ComplaintManagementController {

    @Autowired
    private IComplaintManagementService complaintManagementService;

    /**
     查询投诉列表
     */
    @PostMapping("/list")
    public ResponseEntity<Map<Object, Object>> list(@RequestBody ComplaintManagement complaintManagement) {
        Map<Object,Object> response = new HashMap<>();
        List<ComplaintManagement> complaintManagements = complaintManagementService.selectComplaintManagementList(complaintManagement);
        if (!complaintManagements.isEmpty()) {
            response.put("code", 200);
            response.put("msg", "查询成功");
            response.put("data", complaintManagements);
        } else {
            response.put("code", 401);
            response.put("msg", "无投诉信息");
        }
        return ResponseEntity.ok(response);
    }

    /**
     投诉
     */
    @PostMapping("/insert")
    public ResponseEntity<Map<Object, Object>> insert(@RequestBody ComplaintManagement complaintManagement) {
        Map<Object,Object> response = new HashMap<>();
        if (complaintManagement.getRemarks() == null) {
            complaintManagement.setRemarks("无");
        }
        complaintManagement.setComplaintDate(CurrentDate.convertLocalDateToDate(LocalDate.now()));
        complaintManagement.setComplaintTime(CurrentDate.convertLocalDateTimeToDate(LocalDateTime.now()));
        int i = complaintManagementService.insertComplaintManagement(complaintManagement);
        if (i != 0) {
            response.put("code", 200);
            response.put("msg", "投诉成功");
        } else {
            response.put("code", 400);
            response.put("msg", "投诉失败");
        }
        return ResponseEntity.ok(response);
    }

}
